<template>
  <div class="manage">
    <el-button size="mini" @click="goHome">首页</el-button>
    <div class="form-top">
      <el-button type="primary" icon="el-icon-plus" @click="addUserData"
        >新增</el-button
      >
      <div class="from-rigth">
        <el-input
          placeholder="请输入内容"
          prefix-icon="el-icon-search"
          clearable
          v-model="userForm.name"
        ></el-input>
        <el-button type="primary" @click="seachUser">搜索</el-button>
      </div>
    </div>
    <!-- 表格 -->
    <el-table
      :data="tableData"
      border
      style="width: 100%; margin-top: 20px"
      height="85%"
    >
      <el-table-column prop="name" label="姓名" width="180" align="center">
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="180" align="center">
      </el-table-column>
      <el-table-column prop="sex" label="性别" width="180" align="center">
        <template slot-scope="scope">
          {{ scope.row.sex == 1 ? "男" : "女" }}
        </template>
      </el-table-column>
      <el-table-column prop="birth" label="出生日期" align="center">
      </el-table-column>
      <el-table-column prop="addr" label="地址" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="warning"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- //分页器 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="handlePage"
    >
    </el-pagination>
    <!-- 弹出框 -->
    <el-dialog
      :title="modelType == 0 ? '新增用户' : '修改用户'"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <el-form
        ref="form"
        :model="formData"
        label-width="80px"
        :rules="FormRules"
        inline
      >
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formData.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="formData.age" placeholder="请输入年龄"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="formData.sex" placeholder="请选择">
            <el-option label="男" :value="1"></el-option>
            <el-option label="女" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期" prop="birth">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="formData.birth"
            value-format="yyyy-MM-DD"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="地址" prop="addr">
          <el-input v-model="formData.addr" placeholder="请输入地址"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getList, addUser, editUser, delUser } from "../api";
export default {
  data() {
    return {
      userForm: {
        name: "",
      },
      modelType: 0,
      dialogVisible: false,
      tableData: [],
      formData: {
        name: "",
        age: "",
        sex: "",
        birth: "",
        addr: "",
      },

      //表单验证
      FormRules: {
        name: [
          {
            required: true,
            message: "请输入正确的名称",
            trigger: "blur",
          },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        age: [
          {
            required: true,
            message: "请输入正确的年龄",
            trigger: "blur",
          },
          { min: 1, max: 3, message: "长度在 1 到 3 个字符", trigger: "blur" },
        ],
        sex: [
          {
            required: true,
            message: "请选择性别",
            trigger: "blur",
          },
        ],
        birth: [
          {
            required: true,
            message: "请选择日期",
            trigger: "blur",
          },
        ],
        addr: [
          { required: true, message: "请输入正确的地址", trigger: "blur" },
          {
            min: 5,
            max: 30,
            message: "长度在 5 到 30 个字符",
            trigger: "blur",
          },
        ],
      },
      total: 0,
      pageData: {
        page: 1,
        limit: 10,
      },
    };
  },
  methods: {
    goHome() {
      this.$router.push("/home");
    },
    async getUserList() {
      const res = await getList({
        params: { ...this.userForm, ...this.pageData },
      });
      try {
        if (res.code === 20000) {
          this.tableData = res.list;
          this.total = res.count || 0;
        }
      } catch (error) {
        return this.$message(error);
      }
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
    },
    cancel() {
      this.handleClose();
    },
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.modelType == 0) {
            addUser(this.formData).then(() => {
              this.getUserList();
              this.$refs.form.resetFields();
              this.dialogVisible = false;
            });
          } else {
            editUser(this.formData).then(() => {
              this.getUserList();
              this.$refs.form.resetFields();
              this.dialogVisible = false;
            });
          }
        } else {
          this.$message.error("请填写正确的表单信息");
        }
      });
    },
    //新增按钮回调
    addUserData() {
      this.dialogVisible = true;
      this.modelType = 0;
    },
    //编辑按钮
    handleEdit(data) {
      this.dialogVisible = true;
      this.modelType = 1;
      this.formData = JSON.parse(JSON.stringify(data));
    },
    //删除
    handleDelete(row) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          delUser({ id: row.id }).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getUserList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handlePage(val) {
      this.pageData.page = val;
      this.getUserList();
    },
    //搜索用户
    seachUser() {
      this.getUserList();
    },
  },
  mounted() {
    this.getUserList();
  },
  watch: {
    userForm: {
      deep: true,
      handler() {
        if (this.userForm.name == "") {
          this.getUserList();
        }
      },
    },
  },
};
</script>

<style lang="less" scoped>
.form-top {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;

  .from-rigth {
    display: flex;
    justify-content: space-between;

    .el-button {
      margin: 0 20px;
    }
  }
}
.manage {
  height: 90%;
}
.el-pagination {
  text-align: center;
  margin-top: 20px;
}
</style>